<?php

namespace App\Providers;

use App\Models\Observer\PatientObserver;
use App\Models\Observer\UserObserver;
use App\Models\Patient;
use App\Project\Auth\Provider\DoctorUserProvider;
use App\Project\Auth\Provider\PatientUserProvider;
use App\User;
use Faker\Factory as FakerFactory;
use Faker\Generator as FakerGenerator;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\Validator;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     * @return void
     */
    public function boot()
    {
        if (config('app.debug')) {
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
            $this->app->register(\Barryvdh\Debugbar\ServiceProvider::class);
            $this->app->register(\Laravel\Tinker\TinkerServiceProvider::class);
        }

        $this->app->register(MedicalSceneProvider::class);

        User::observe(UserObserver::class);
        Patient::observe(PatientObserver::class);


        \Auth::provider('doctors', function ($app, array $config) {

            return new DoctorUserProvider($app->make('hash'), User::class);
        });

        \Auth::provider('patients', function ($app, array $config) {

            return new PatientUserProvider($app->make('hash'), Patient::class);
        });

        env('APP_DEBUG') && $this->app->singleton(FakerGenerator::class, function () {
            return FakerFactory::create('zh_CN');
        });

        \Carbon\Carbon::setLocale('zh');

        //  拓展小数验证
        \Validator::extend('decimal', function ($attribute, $value, $parameters, $validator) {

            if (!is_numeric($value)) {
                return false;
            }

            $parameters[1] = isset($parameters[1]) ? $parameters[1] : false;

            $position = strpos(strrev((string)$value), '.');

            if (0 === $position) {
                return false;
            }

            if (false !== $position) {
                return $parameters[1] ? $position == $parameters[0] : $position <= $parameters[0];
            } elseif ($parameters[1]) {
                return false;
            }

            return true;
        });

        \Validator::extendDependent('greater_than', function ($attribute, $value, $parameters, Validator $validator) {
            return $value > Arr::get($validator->getData(), $parameters[0]);
        });

        \Validator::extendDependent('less_than', function ($attribute, $value, $parameters, Validator $validator) {
            return $value < Arr::get($validator->getData(), $parameters[0]);
        });

        //  除非出现某个字段，或某个字段的值为特定值时，才能提交当前字段 (不支持 参数中有分号)
        \Validator::extend('import_unless', function ($attribute, $value, $parameters, Validator $validator) {

            $input = $validator->getData();

            if (!Arr::has($input, $parameters[0])) {
                return false;
            }

            $other = Arr::get($input, $parameters[0]);

            if (('' === $other) || is_null($other) || ($other == $parameters[1])) {
                return true;
            }

            return false;
        });
        //  除非出现某个字段，且这个字段值不等于给定的值，才能提交当前字段 (不支持 参数中有分号)
        \Validator::extend('import_unless_not', function ($attribute, $value, $parameters, Validator $validator) {

            $input = $validator->getData();

            if (!Arr::has($input, $parameters[0])) {
                return false;
            }

            $other = Arr::get($input, $parameters[0]);

            if (('' === $other) || is_null($other) || ($other != $parameters[1])) {
                return true;
            }

            return false;
        });
    }

    /**
     * Register any application services.
     * @return void
     */
    public function register()
    {
        //

    }
}
